package com.guojian.jdbc.annotation;

import java.util.Map;

import javax.persistence.Column;
import javax.persistence.EntityManager;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jdbc.repository.support.SimpleJdbcRepository;
import org.springframework.data.jpa.repository.support.SimpleJpaRepository;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import com.guojian.jdbc.jpa.Test;
import com.guojian.jdbc.sql.SqlExpression;

import lombok.Data;

@Data
@Table(name = "test_e")
@Component
public class TestBean {

	private DataSource dataSource;

	@Autowired
	private JdbcTemplate JdbcTemplate;//实际注入的是com.guojian.jdbc.JdbcTemplateX@47ef5a33,注入的是子类

	//@Autowired
	private SimpleJpaRepository<Test, String> jpa;

	//@Autowired
	private EntityManager em;

	//@Autowired
	private SimpleJdbcRepository<Test, String> jdbc;

	public TestBean() {
	}

	public TestBean(DataSource dataSource) {
		super();
		this.dataSource = dataSource;
	}

	public TestBean(DataSource dataSource, JdbcTemplate JdbcTemplate) {
		super();
		this.dataSource = dataSource;
		this.JdbcTemplate = JdbcTemplate;
	}

	@SqlExp(alias = "a", exp = SqlExpression.LIKE)
	@Column(name = "name")
	@GeneratedValue(strategy = GenerationType.AUTO)
	private String name;

	@SqlExp(alias = "a", exp = SqlExpression.EQUAL)
	@GeneratedValue(strategy = GenerationType.AUTO, generator = "test")
	@Id
	private String id;

	@SqlExp
	private Integer age;

	@SqlExp(exp = SqlExpression.EQUAL_OR_GREATER)
	@Temporal(value = TemporalType.TIMESTAMP)
	private String beginDate;

	@SqlExp(exp = SqlExpression.EQUAL_OR_LESSER)
	@Temporal(value = TemporalType.DATE)
	private String endDate;

	private Map<String, String> attribute;

	//@PostConstruct
	public void say() {
		System.out.println(this.getDataSource() + "---" + this.getJdbcTemplate());
		//HikariDataSource (HikariPool-1)---com.guojian.jdbc.JdbcTemplateX@47ef5a33
		/*List<Test> testList = this.getJpa().findAll();
		System.out.println(testList);
		System.out.println(em);*/

		Test test = new Test();
		//test.setName("guojian");
		//test.setId(1000);

		Test test1111 = this.getJdbc().save(test);
		System.out.println(test1111);

	}

}
